#!/usr/bin/env bash

# Consolidated installation script for use by both Travis and humans.
#
# First installs a known-good version of pip, then any requirements
# specified in the EXTERNAL_REQUIREMENTS environment variable (e.g.,
# coveralls); then installs the project requirements, constrained by
# etc/requirements.txt; then editably installs zipline itself.
#
# Forwards positional arguments to all invocations of pip install.

# Travis' env command doesn't permit options in the shebang line, so
# set them here.
set -euvxo pipefail

echo
echo "Installing zipline using $(which python)"
echo

# New releases of pip have frequently caused strange issues. Make sure
# we know exactly which version we're working with.
python -m pip install pip==19.2.2 $@

# Install external requirements first: if they share any of our
# transitive dependencies, we want our pinned versions to win.
if [ "${EXTERNAL_REQUIREMENTS:-}" ]; then
    # Note: If EXTERNAL_REQUIREMENTS is unset, the expression in the
    # above test expands to the empty string, which fails the test.
    # (Simply expanding $EXTERNAL_REQUIREMENTS causes an error with the
    # -u option, which helps prevent many other kinds of errors.)
    echo "Installing additional packages: $EXTERNAL_REQUIREMENTS"
    python -m pip install "$EXTERNAL_REQUIREMENTS" $@
fi

# These have to be installed first so that the other requirements can be
# compiled against the specific versions we use.
python -m pip install numpy Cython -c etc/requirements.txt $@

# XXX: bcolz has to be compiled against our specific version of numpy:
# by default, it uses an incompatible pre-compiled binary.
python -m pip install --no-binary=bcolz -r etc/requirements.txt -c etc/requirements.txt $@

# TODO: resolve these error messages:
# flake8 3.6.0 has requirement setuptools>=30, but you'll have setuptools 28.8.0 which is incompatible.
# blaze keepalive-30.g31060532 has requirement odo>=0.5.0, but you'll have odo 0.3.2+729.gda7f26d which is incompatible.

python -m pip install -r etc/requirements_dev.txt -c etc/requirements.txt $@
python -m pip install -r etc/requirements_blaze.txt -c etc/requirements.txt $@

# All requirements should already be satisfied by this point.
python -m pip install -e .[all] -c etc/requirements.txt $@

echo
echo "Installation complete! Try running 'zipline --help'."
echo
